<html>
<head><meta charset="utf-8"><title>Unit testing proc macros · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html">Unit testing proc macros</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196688891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196688891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196688891">(May 06 2020 at 20:00)</a>:</h4>
<p>Hello! I'm working on a proc macro, and I'm writing unit tests for it (yay for proc_macro2). I'm writing tests for my error cases, so I'm trying to scan through a TokenStream and find all invocations of <code>compile_error!{...}</code>. Is there any easy way to do that?</p>
<p>To be more specific, my proc macro tries to check as many conditions as it can in a single invocation, so it returns a TokenStream that contains numerous syn::Error objects, which get translated to <code>compile_error!{...}</code> invocations. This way, the developer sees all of the errors for their code at once, rather than fixing one error, recompiling, then finding the next error, etc.</p>
<p>So in my unit tests, I have a bunch of known-bad inputs, I run them through my macro code, and now I need to find the <code>compile_error!</code> invocations. I'm a bit stuck at finding an easy / attractive way to scan through a TokenStream, looking for a particular sequence of tokens, though. All of the parsing APIs are structured around matching and consuming tokens, not skipping through a TokenStream looking for a pattern.</p>
<p>This sounds like something someone has probably already done, so I thought I would ask.</p>



<a name="196692323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196692323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196692323">(May 06 2020 at 20:26)</a>:</h4>
<p>I am not sure about the specific details of what you said, but when the question is "testing errors of proc macros" my first thought is <a href="https://crates.io/crates/trybuild" title="https://crates.io/crates/trybuild">https://crates.io/crates/trybuild</a></p>



<a name="196692380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196692380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196692380">(May 06 2020 at 20:27)</a>:</h4>
<p>I haven't used that crate myself (not a proc macro author) but it looks pretty cool, and it's by dtolnay so I'm sure it's going to work very well</p>



<a name="196692409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196692409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196692409">(May 06 2020 at 20:27)</a>:</h4>
<p>Interesting. I'll take a look.</p>



<a name="196705304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196705304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196705304">(May 06 2020 at 22:15)</a>:</h4>
<p>Yes this is what I do. <a href="https://github.com/shepmaster/snafu/tree/master/compatibility-tests/compile-fail" title="https://github.com/shepmaster/snafu/tree/master/compatibility-tests/compile-fail">https://github.com/shepmaster/snafu/tree/master/compatibility-tests/compile-fail</a></p>



<a name="196705334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196705334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196705334">(May 06 2020 at 22:15)</a>:</h4>
<p>So it’s definitely not unit tests.</p>



<a name="196705382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196705382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196705382">(May 06 2020 at 22:16)</a>:</h4>
<p>I found that to be too heavy-weight: writing files to disk, spinning up the entire compiler. I found a way to quickly find the compile_error!(...) invocations in my token stream, which, for my purposes, is all I need.</p>



<a name="196744032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196744032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196744032">(May 07 2020 at 09:06)</a>:</h4>
<p>You might want to check <code>proc-macro-error</code> crate. It is great instead of directly using <code>compile_error</code></p>



<a name="196744037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20testing%20proc%20macros/near/196744037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20testing.20proc.20macros.html#196744037">(May 07 2020 at 09:06)</a>:</h4>
<p>We are using it in <code>clap</code> and <code>structopt</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>